package org.kjtc.controller;

import org.kjtc.entity.User;
import org.kjtc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ Author     ：Yellowong
 * @ Date       ：Created in 9:24 2018/12/5
 * @ Description：
 */
@Controller
public class LoginController {

    @Autowired
    UserService userService;

    /* 单独写一个controller不划算，暂时都先放在这里响应请求 */
    @RequestMapping("")
    public String login(){
        return "login";
    }

    @RequestMapping("/main")
    public String mainPage(){
        return "main";
    }

    @RequestMapping("/overview")
    public String overviewPage(){
        return "overview";
    }

    @RequestMapping("/monthlyBillCheck")
    public String monthlyBillCheckPage(){
        return "monthlyBillCheck";
    }

    @RequestMapping("/login")
    public String loginPage(){
        return "login";
    }

    @RequestMapping("/loginCheck")
    @ResponseBody
    public String loginCheck(HttpServletRequest request, HttpServletResponse response, User user) {

        String result = null;   // 登录结果

        /* 简易的登录验证 */
        User DBUser = userService.getUser(user);
        if (DBUser == null) {   // 账号不存在的情况
            result = "notExist";
        } else {
            if (!user.getPsword().equals(DBUser.getPsword())) {  // 密码错误的情况
                result = "wrongPsword";
            } else {    // 登录成功的情况
                result = "success";
            }
        }

        return result;
    }
}
